package org.xxpay.manage.mq.topic;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.manage.common.service.CommonService;

import javax.jms.Topic;

@Component
public class MqTopic4ModifySysConfig {

    private static final MyLog _log = MyLog.getLog(MqTopic4ModifySysConfig.class);

    @Autowired private JmsTemplate jmsTemplate;
    @Autowired private Topic modifySysConfigTopic;
    @Autowired private CommonService commonService;

    /** 接收 更新系统配置项的消息 **/
    @JmsListener(destination = MchConstant.COMMON_MQ_TOPIC.MODIFY_SYS_CONFIG, containerFactory = "jmsListenerContainer")
    public void receive(String msg) {

        _log.info("成功接收更新系统配置的订阅通知, msg={}", msg);
        commonService.sysConfigService.initDBApplicationConfig(msg);
        _log.info("系统配置静态属性已重置");
    }


    /** 推送消息到各个节点 **/
    public void push(String msg) {
        this.jmsTemplate.convertAndSend(modifySysConfigTopic, msg);
    }

    @Bean
    public Topic modifySysConfigTopic(){
        return new ActiveMQTopic(MchConstant.COMMON_MQ_TOPIC.MODIFY_SYS_CONFIG);
    }

}
